/*
* This software is licensed under the GNU General Public License, version 2
* You may copy, distribute and modify the software as long as you track changes/dates of in source files and keep all modifications under GPL. You can distribute your application using a GPL library commercially, but you must also provide the source code.

* DNNspot Software (http://www.dnnspot.com)
* Copyright (C) 2013 Atriage Software LLC
* Authors: Kevin Southworth, Matthew Hall, Ryan Doom

* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.

* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.

* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

* Full license viewable here: http://www.gnu.org/licenses/gpl-2.0.txt
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace DNNspot.LiveChat.DataModel
{
    public partial class Category
    {
        public bool IsChildCategoryOf(int? categoryId)
        {
            if (categoryId == null)
                return false;

            var category = this;

            while(category.ParentCategoryId != null)
            {
                if(category.ParentCategoryId == categoryId)
                    return true;
                
                category = Load(category.ParentCategoryId);
            }

            return false;
        }

        public static Category Load(int? categoryId)
        {
            var category = new Category();

            if (categoryId != null)
                category.LoadByPrimaryKey(Convert.ToInt32(categoryId));

            return category;
        }

        public string GetTreeName()
        {
            var category = this;
            var parentId = ParentCategoryId;
            var text = Name;

            while (category.ParentCategoryId != null)
            {
                category = Load(Convert.ToInt32(parentId));
                parentId = category.ParentCategoryId;

                text = HttpContext.Current.Server.HtmlDecode(string.Format("&#160;&#160;&#160;&#160;{0}", text));
            }

            return text;
        }

        public string GetFullPath()
        {
            var category = this;
            var parentId = ParentCategoryId;
            var text = Name;

            while (category.ParentCategoryId != null)
            {
                category = Load(Convert.ToInt32(parentId));
                parentId = category.ParentCategoryId;

                text = string.Format("{0} > {1}", category.Name, text);
            }

            return text;
        }
    }

    public partial class CategoryCollection
    {
        public static int GetTotal(FlexigridSearchCriteria searchCriteria)
        {
            var tmpNumber = searchCriteria.PageNumber;
            var tmpSize = searchCriteria.PageSize;

            searchCriteria.PageNumber = -1;
            searchCriteria.PageSize = -1;

            var count = Search(searchCriteria).Count;

            searchCriteria.PageNumber = tmpNumber;
            searchCriteria.PageSize = tmpSize;

            return count;
        }

        public static void Initialize(int portalId)
        {
            var q = new CategoryQuery();
            q.Where(q.PortalId == portalId);

            var categories = new CategoryCollection();
            if(!categories.Load(q))
            {
                var generalCategory = new Category();
                generalCategory.PortalId = portalId;
                generalCategory.Name = "General";
                generalCategory.Save();
            }
        }

        public static List<Category> Search(FlexigridSearchCriteria searchCriteria)
        {
            var q = new CategoryQuery();

            q.es.PageNumber = searchCriteria.PageNumber;
            q.es.PageSize = searchCriteria.PageSize;

            q.Where(q.PortalId == searchCriteria.PortalId);

            foreach (String keyword in searchCriteria.Query.Split(' '))
            {
                q.And(string.Format("{0} LIKE '%{1}%'", searchCriteria.QueryColumns, keyword));
            }

            q.OrderBy(searchCriteria.SortColumn, searchCriteria.SortDirection);

            var categories = new CategoryCollection();
            categories.Load(q);

            return categories.ToList();
        }

        public List<Category> SortByParentTree()
        {
            var catStack = new List<Category>();
            var catList = new List<Category>();
            
            foreach (Category category in this)
                catStack.Add(category);

            catStack.OrderBy(c => c.Name);

            while(catStack.Count > 0)
            {
                if(!catList.Contains(catStack[0]))
                    catList.Add(catStack[0]);

                catList.InsertRange(
                    catList.IndexOf(catStack[0]) + 1, 
                    catStack.FindAll(c => c.ParentCategoryId == catStack[0].CategoryId).OrderBy(c => c.Name));

                catStack.RemoveAt(0);
            }

            return catList;
        }
    }
}
